package com.chan.net;

import android.net.ParseException;

import com.base.pool.ObjectPool;
import com.base.pool.ObjectPoolItem;

import org.json.JSONException;

import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.UnknownHostException;

import io.reactivex.functions.Consumer;
import retrofit2.HttpException;

/**
 * 仿glide的对象池封装的Observer对象池
 */
public class DefaultRxConsumer implements Consumer<Result>, ObjectPoolItem {

    private HttpCallback httpCallback;
    private String requestCommand;

    public static DefaultRxConsumer obtain() {
        DefaultRxConsumer observer = ObjectPool.obtain(DefaultRxConsumer.class);
        if (observer == null) {
            observer = new DefaultRxConsumer();
        }
        return observer;
    }

    private DefaultRxConsumer() {

    }

    /**
     * 对象池重用
     *
     * @param requestCommand
     * @param httpCallback
     * @return
     */
    public static DefaultRxConsumer obtain(String requestCommand, HttpCallback httpCallback) {
        DefaultRxConsumer observer = obtain();
        observer.init(requestCommand, httpCallback);
        return observer;
    }

    private synchronized void init(String requestCommand, HttpCallback httpCallback) {
        this.httpCallback = httpCallback;
        this.requestCommand = requestCommand;
    }

    /**
     * 清空销毁
     */
    @Override
    public synchronized void recycle() {
        httpCallback = null;
        requestCommand = null;
        ObjectPool.recycle(this);
    }

    @Override
    public void accept(Result result) throws Exception {
        try {
            if (result.code == 0) {
                if (httpCallback != null) {
                    if (result.data != null) {
                        httpCallback.onSuccess(requestCommand, result);
                    } else {
                        httpCallback.onFail(requestCommand, Result.FAILCODE.CODE_DATA_EMPTY, "数据为null");
                    }
                }
            } else {
                if (httpCallback != null) {
                    httpCallback.onFail(requestCommand, result.code, result.message);
                }
            }
            recycle();
        } catch (Exception e) {
            int code;
            String msg;
            if (e instanceof HttpException) {     //   HTTP错误
                code = Result.FAILCODE.CODE_BAD_NETWORK;
                msg = "HTTP错误";
            } else if (e instanceof ConnectException
                    || e instanceof UnknownHostException) {   //   连接错误
                code = Result.FAILCODE.CODE_CONNECT_ERROR;
                msg = "连接错误";
            } else if (e instanceof InterruptedIOException) {   //  连接超时
                code = Result.FAILCODE.CODE_CONNECT_TIMEOUT;
                msg = "连接超时";
            } else if (e instanceof JSONException
                    || e instanceof ParseException) {   //  解析错误
                code = Result.FAILCODE.CODE_PARSE_ERROR;
                msg = "解析错误";
            } else {
                code = Result.FAILCODE.CODE_UNKNOWN_ERROR;
                msg = "位置错误";
            }
            if (httpCallback != null) {
                httpCallback.onFail(requestCommand, code, msg);
            }
        }
    }
}
